package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.mapper.BrandMapper;
import com.li.youhappygo.mapper.CategoryBrandMapper;
import com.li.youhappygo.mapper.CategoryMapper;
import com.li.youhappygo.model.entity.commond.Brand;
import com.li.youhappygo.model.entity.commond.BrandCategory;
import com.li.youhappygo.model.entity.commond.Category;
import com.li.youhappygo.model.vo.category.CategoryBrandVo;
import com.li.youhappygo.service.CategoryBrandService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryBrandServiceImpl implements CategoryBrandService {

    @Autowired
    CategoryBrandMapper categoryBrandMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public PageInfo<CategoryBrandVo> getCategoryBrand(Integer pageNum, Integer page, BrandCategory brandCategory) {
        PageHelper.startPage(pageNum, page);
        QueryWrapper<BrandCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", brandCategory.getCategoryId());
        List<BrandCategory> brandCategoryList = categoryBrandMapper.selectList(queryWrapper);
        List<CategoryBrandVo> categoryBrandVoList = new ArrayList<>();
        for(BrandCategory brandCategory1 : brandCategoryList){
            CategoryBrandVo categoryBrandVo = new CategoryBrandVo();
            Brand brand = brandMapper.selectById(brandCategory1.getBrandId());
            Category category = categoryMapper.selectById(brandCategory1.getCategoryId());
            BeanUtils.copyProperties(brandCategory,categoryBrandVo);
//            categoryBrandVo.setCategoryId(brandCategory1.getCategoryId());
            categoryBrandVo.setBrandName(brand.getName());
            categoryBrandVo.setCategoryName(category.getName());
            categoryBrandVo.setLogo(brand.getLogoUrl());
            categoryBrandVoList.add(categoryBrandVo);
        }
        return new PageInfo<>(categoryBrandVoList);
    }

    @Override
    public void insertCategoryBrand(BrandCategory brandCategory) {
        categoryBrandMapper.insert(brandCategory);
    }

    @Override
    public List<Brand> findBrandByCategoryId(Long categoryId) {
        QueryWrapper<BrandCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        List<BrandCategory> brandCategories = categoryBrandMapper.selectList(queryWrapper);
        List<Brand> brandList = new ArrayList<>();
        for(BrandCategory brandCategory : brandCategories){
            Brand brand = brandMapper.selectById(brandCategory.getBrandId());
            brandList.add(brand);
        }
        return brandList;
    }

    @Override
    public void deleteCategoryBrand(BrandCategory brandCategory) {
        QueryWrapper<BrandCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",brandCategory.getCategoryId());
        queryWrapper.eq("brand_id",brandCategory.getBrandId());
        categoryBrandMapper.delete(queryWrapper);
    }


}
